﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using Gem_Tools_Entity.Gem_Tools;
using Gem_Tools_DataLayer;
using Gem_Tools_Utility;

namespace Gem_Tools_Controller.Gem_Tools
{
    public class CStage_Or_Part_Controller
    {
        private void MapStage_Or_Part(DataRow p_row, CStage_Or_Part p_objDes)
        {
            DataTable p_dt = p_row.Table;

            if (p_dt.Columns.Contains("Auto_ID"))
                p_objDes.Auto_ID = CUtility.Convert_To_Int32(p_row["Auto_ID"]);

            if (p_dt.Columns.Contains("Pharse_ID"))
                p_objDes.Pharse_ID = CUtility.Convert_To_Int32(p_row["Pharse_ID"]);

            if (p_dt.Columns.Contains("Parent_ID"))
                p_objDes.Parent_ID = CUtility.Convert_To_Int32(p_row["Parent_ID"]);

            if (p_dt.Columns.Contains("Name"))
                p_objDes.Name = CUtility.Convert_To_String(p_row["Name"]);

            if (p_dt.Columns.Contains("Art_Leader_ID"))
                p_objDes.Art_Leader_ID = CUtility.Convert_To_Int32(p_row["Art_Leader_ID"]);

            if (p_dt.Columns.Contains("URL"))
                p_objDes.URL = CUtility.Convert_To_String(p_row["URL"]);

            if (p_dt.Columns.Contains("Priority"))
                p_objDes.Priority = CUtility.Convert_To_Int32(p_row["Priority"]);

            if (p_dt.Columns.Contains("Start_Date"))
                p_objDes.Start_Date = CUtility.Convert_To_DateTime(p_row["Start_Date"]);

            if (p_dt.Columns.Contains("End_Date"))
                p_objDes.End_Date = CUtility.Convert_To_DateTime(p_row["End_Date"]);

            if (p_dt.Columns.Contains("Status"))
                p_objDes.Status = CUtility.Convert_To_Int32(p_row["Status"]);

            if (p_dt.Columns.Contains("Created"))
                p_objDes.Created = CUtility.Convert_To_DateTime(p_row["Created"]);

            if (p_dt.Columns.Contains("Created_By"))
                p_objDes.Created_By = CUtility.Convert_To_String(p_row["Created_By"]);

            if (p_dt.Columns.Contains("Deleted"))
                p_objDes.Deleted = CUtility.Convert_To_Int32(p_row["Deleted"]);

            if (p_dt.Columns.Contains("Last_Updated"))
                p_objDes.Last_Updated = CUtility.Convert_To_DateTime(p_row["Last_Updated"]);

            if (p_dt.Columns.Contains("Last_Updated_By"))
                p_objDes.Last_Updated_By = CUtility.Convert_To_String(p_row["Last_Updated_By"]);

        }

        public IList<CStage_Or_Part> List_Stage_Or_Part()
        {
            IList<CStage_Or_Part> v_arrStage_Or_Part = new List<CStage_Or_Part>();
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "sp_sel_List_Stage_Or_Part");

                foreach (DataRow v_row in v_dt.Rows)
                {
                    CStage_Or_Part v_objStage_Or_Part = new CStage_Or_Part();
                    MapStage_Or_Part(v_row, v_objStage_Or_Part);
                    v_arrStage_Or_Part.Add(v_objStage_Or_Part);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_arrStage_Or_Part;
        }

        public CStage_Or_Part Get_Stage_Or_Part_By_ID(int p_iID)
        {
            CStage_Or_Part v_objStage_Or_Part = null;
            DataTable v_dt = new DataTable();

            try
            {
                CSqlHelper.FillDataTable(CConfig.g_strGem_Tools_Conn_String, v_dt, "sp_sel_Get_Stage_Or_Part_By_ID", p_iID);

                if (v_dt.Rows.Count > 0)
                {
                    v_objStage_Or_Part = new CStage_Or_Part();
                    MapStage_Or_Part(v_dt.Rows[0], v_objStage_Or_Part);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                v_dt.Dispose();
            }

            return v_objStage_Or_Part;
        }

        public int Insert_Stage_Or_Part(CStage_Or_Part p_objData)
        {
            int v_iRes = CConst.INT_VALUE_NULL;

            try
            {
                v_iRes = Convert.ToInt32(CSqlHelper.ExecuteScalar(CConfig.g_strGem_Tools_Conn_String, "sp_ins_Stage_Or_Part",
                            p_objData.Pharse_ID, p_objData.Parent_ID, p_objData.Name, p_objData.Art_Leader_ID, p_objData.URL, p_objData.Priority, p_objData.Start_Date, p_objData.End_Date, p_objData.Status, p_objData.Last_Updated_By).ToString());

            }
            catch (Exception ex)
            {
                throw ex;
            }


            return v_iRes;
        }

        public void Update_Stage_Or_Part(CStage_Or_Part p_objData)
        {
            try
            {
                CSqlHelper.ExecuteNonquery(CConfig.g_strGem_Tools_Conn_String, "sp_upd_Stage_Or_Part",
                            p_objData.Auto_ID, p_objData.Pharse_ID, p_objData.Parent_ID, p_objData.Name, p_objData.Art_Leader_ID, p_objData.URL, p_objData.Priority, p_objData.Start_Date, p_objData.End_Date, p_objData.Status, p_objData.Last_Updated_By);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete_Stage_Or_Part(int p_iID, string p_strLast_Updated_User)
        {
            try
            {
                CSqlHelper.ExecuteNonquery(CConfig.g_strGem_Tools_Conn_String, "sp_del_Stage_Or_Part", p_iID, p_strLast_Updated_User);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
